package copyengine.ui.component.button.animation
{
    import copyengine.ui.display.movieClip.CEMovieClip;

    import starling.display.DisplayObject;
    import starling.display.DisplayObjectContainer;
    import starling.events.Touch;
    import starling.filters.ColorMatrixFilter;

    public class CEFrameSelectableButtonAnimation implements ICESelectableButtonAnimation
    {
        private static const FRAME_SELECTED:String = "s";
        private static const FRAME_UNSELECTED:String = "unS";

        private var mTarget:CEMovieClip;
        private var mIsEnable:Boolean = true;
        private var mTouchDownFilter:ColorMatrixFilter;
        private var mDisableFilter:ColorMatrixFilter;

        public function CEFrameSelectableButtonAnimation() {
        }

        public function onSelectedChange(_isSelected:Boolean):void {
            mTarget.gotoAndStop(_isSelected == true ? FRAME_SELECTED : FRAME_UNSELECTED);
        }

        public function initialize(_target:DisplayObject):void {
            var t:DisplayObjectContainer = _target as DisplayObjectContainer;
            var bg:CEMovieClip = t.getChildByName("bg") as CEMovieClip;
            if (bg != null) {
                mTarget = bg;
            } else {
                mTarget = t as CEMovieClip;
            }

            mTouchDownFilter = new ColorMatrixFilter();
            mTouchDownFilter.adjustBrightness(-0.3); //亮度

            mDisableFilter = new ColorMatrixFilter();
            mDisableFilter.adjustSaturation(-1);
        }

        public function dispose():void {
            mTouchDownFilter && mTouchDownFilter.dispose();
            mDisableFilter && mDisableFilter.dispose();
        }

        public function onTouchDown(_touch:Touch):void { mIsEnable == true ? mTarget.filter = mTouchDownFilter : null; }

        public function onTouchUp(_touch:Touch):void { mIsEnable == true ? mTarget.filter = null : null; }

        public function set isEnabled(_value:Boolean):void {
            mTarget.filter = _value == false ? mDisableFilter : null;
            mIsEnable = _value;
        }

    }
}
